﻿using common;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Dao
{
    public class CartDao
    {
        private string sqlStr;
        /// <summary>
        /// 将商品添加到购物车
        /// </summary>
        /// <param name="cart"></param>
        /// <returns>一个完整的Cart实体</returns>
        public bool AddToCart(Cart cart)
        {
            sqlStr = "insert into cart (userId, bookId, num) values (@userId, @bookId, @num)";
            SqlParameter[] parameters =
            {
                new SqlParameter("@userId", SqlDbType.Int),
                new SqlParameter("@bookId", SqlDbType.Int),
                 new SqlParameter("@num", SqlDbType.Int)
            };
            parameters[0].Value = cart.User.Id;
            parameters[1].Value = cart.Book.Id;
            parameters[2].Value = cart.Num;
            return SqlHelper.ExecNonQuery(sqlStr, parameters) > 0;
        }

        /// <summary>
        /// 更新购物车
        /// </summary>
        /// <param name="cart"></param>
        /// <returns>一个完整的Cart实体</returns>
        public bool UpdateCart(Cart cart)
        {
            sqlStr = "update cart  set num=@num where id=@cartId";
            SqlParameter[] parameters =
            {
                new SqlParameter("@num", SqlDbType.Int),
                new SqlParameter("@cartId", SqlDbType.Int)
            };
            parameters[0].Value = cart.Num;
            parameters[1].Value = cart.Id;
            return SqlHelper.ExecNonQuery(sqlStr, parameters) > 0;
        }

        /// <summary>
        /// 更新购物车
        /// </summary>
        /// <param name="cartId"></param>
        /// <returns>一个完整的Cart实体</returns>
        public bool DeleteCartById(int cartId)
        {
            sqlStr = "delete from cart where id = @id";
            SqlParameter[] parameters =
            {
                new SqlParameter("@id", cartId),
            };
            return SqlHelper.ExecNonQuery(sqlStr, parameters) > 0;
        }

        /// <summary>
        /// 通过用户Id获取该用户的购物车列表
        /// </summary>
        /// <param name="AccountId">用户Id</param>
        public List<Cart> GetCartByAccountId(int AccountId)
        {
            sqlStr = "select a.id as id,b.id as bookId,userId,name,price, image, num,oneword,author from cart as a, bookInfo as b where a.userId=@AccountId and a.bookId=b.id";

            SqlParameter[] parameters =
            {
                new SqlParameter("@accountId", AccountId),
            };

            DataTable dataTable = SqlHelper.GetDataTable(sqlStr, parameters);
            List<Cart> list = null;
            if (dataTable.Rows.Count > 0)
            {
                list = new List<Cart>();
                foreach (DataRow row in dataTable.Rows)
                {
                    Cart cart = new Cart();
                    LoadEntity(cart, row);
                    list.Add(cart);
                }
            }
            return list;
        }


        /// <summary>
        /// 通过用户ID和购物车序号查询购物车详情
        /// </summary>
        /// <param name="AccountId">用户Id</param>
        /// <param name="CartId">购物车序号</param>
        public List<Cart> GetCartById(int CartId,int AccountId)
        {
            sqlStr = "select a.id as id,b.id as bookId,userId,name,price, image, num,oneword,author from cart as a, bookInfo as b where a.userId=@AccountId and a.bookId=b.id and a.id=@cartId";

            SqlParameter[] parameters =
            {
                new SqlParameter("@accountId", AccountId),
                 new SqlParameter("@cartId", CartId)
            };

            DataTable dataTable = SqlHelper.GetDataTable(sqlStr, parameters);
            List<Cart> list = null;
            if (dataTable.Rows.Count > 0)
            {
                list = new List<Cart>();
                foreach (DataRow row in dataTable.Rows)
                {
                    Cart cart = new Cart();
                    LoadEntity(cart, row);
                    list.Add(cart);
                }
            }
            return list;
        }

        /// <summary>
        /// 装载数据到Cart
        /// </summary>
        /// <param name="cart">要装载的Cart</param>
        /// <param name="dataRow">数据来源</param>
        private void LoadEntity(Cart cart, DataRow dataRow)
        {
            BookInfo bookInfo = new BookInfo();
            bookInfo.Id = Convert.ToInt32(dataRow["bookId"]);
            bookInfo.Image = dataRow["image"].ToString();
            bookInfo.Name = dataRow["name"].ToString();
            bookInfo.Oneword = dataRow["oneword"].ToString();
            bookInfo.Author = dataRow["author"].ToString();
            bookInfo.Price = Convert.ToDouble(dataRow["price"]);

            Account account = new Account();
            account.Id = Convert.ToInt32(dataRow["bookId"]);

            cart.Id = Convert.ToInt32(dataRow["id"]);
            cart.User = account;
            cart.Book = bookInfo;
            cart.Num = Convert.ToInt32(dataRow["num"]);
        }
    }
}